package com.ding.dailyup.repository;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import com.ding.dailyup.model.StockRecommend;

@Repository
@CacheConfig(cacheNames = "stock:recommend:" + StockRecommend.serialVersionUID)
public interface StockRecommendRepository
		extends JpaRepository<StockRecommend, Long> {

	@Cacheable(key = "'stockRecommend_' + #p0 + '_' + #p2.toString()")
	Optional<List<StockRecommend>> findByStrategyIdAndStatusAndUploadDate(
			Long strategyId, Integer status, LocalDateTime createDate);

	@Modifying
	@Query(nativeQuery = true, value = "update stock_recommend set status = 0 where strategyId = :strategyId and uploadDate = :uploadDate")
	@Caching(evict = {
			@CacheEvict(key = "'stockRecommend_' + #p0 + '_' + #p1.toString()") })
	void delStocksByStrategyIdAndUploadDateAndStockCodes(
			@Param(value = "strategyId") Long strategyId,
			@Param(value = "uploadDate") LocalDateTime uploadDate);
}
